using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;

namespace SampleWorkflows
{
	public class WorkflowsManager
	{
        private static WorkflowsManager m_instance;
        private WorkflowRuntime m_runtime;


        private WorkflowsManager()
        {
        }

        /// <summary>
        /// Gets the runtime.
        /// </summary>
        /// <value>The runtime.</value>
        public WorkflowRuntime Runtime
        {
            get
            {
                if (m_runtime == null)
                    m_runtime = new WorkflowRuntime();
                return m_runtime;
            }
        }

        /// <summary>
        /// Starts the runtime.
        /// </summary>
        public void StartRuntime()
        {
            ManualWorkflowSchedulerService scheduler = new ManualWorkflowSchedulerService();
            this.Runtime.AddService(scheduler);
            
            //Persistence service
            SqlWorkflowPersistenceService persistenceService = new SqlWorkflowPersistenceService(ConfigurationManager.ConnectionStrings["WorkflowPersistence"].ConnectionString);
            this.Runtime.AddService(persistenceService);
            
            //Communication Service
            ExternalDataExchangeService exService = new ExternalDataExchangeService();
            this.Runtime.AddService(exService);
            exService.AddService(PublishVideoService.Instance);
            


            this.Runtime.StartRuntime();
        }

        /// <summary>
        /// Stops the runtime.
        /// </summary>
        public void StopRuntime()
        {
            this.Runtime.StopRuntime();
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static WorkflowsManager Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new WorkflowsManager();
                return m_instance;
            }
        }
        
	}
}
